import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

// 开发环境不使用懒加载, 因为懒加载页面太多的话会造成webpack热更新太慢, 所以只有生产环境使用懒加载
const _import = require('./import-' + process.env.NODE_ENV)

// // 全局路由(无需嵌套上左右整体布局)
// const globalRoutes = [
//   {
//     path: '/main', component: _import('main'), name: 'main', meta: {title: '商城主页'},
//     children: [
//       {path: '/login', component: _import('login'), name: 'login', meta: {title: '登录'}},
//       {path: '/home', component: _import('home/home'), name: 'home', meta: {title: '商城主页'}},
//       {path: '/chat', component: _import('chat/chat'), name: 'chat', meta: {title: '聊天'}},
//       {path: '/cart', component: _import('cart/cart'), name: 'cart', meta: {title: '购物车'}},
//       {path: '/user', component: _import('user/user'), name: 'user', meta: {title: '用户主页'}}
//     ]
//   },
//   {path: '/', redirect: '/home'},
// ]


// 全局路由(无需嵌套上左右整体布局)
const globalRoutes = [
  // { path: '/404', component: _import('common/404'), name: '404', meta: { title: '404未找到' } },
  {path: '/login', component: _import('login'), name: 'login', meta: {title: '登录'}},
  // { path: '/shopMain', component: _import('shop/showMain'), name: 'shopMain', meta: { title: '商城主页' } }
]

// 主入口路由(需嵌套上左右整体布局)
const mainRoutes = {
  path: '/',
  component: _import('main'),
  name: 'main',
  redirect: {name: 'home'},
  meta: {title: '主入口整体布局'},
  children: [
    {path: '/home', component: _import('home/home'), name: 'home', meta: {title: '商城主页'}},
    {path: '/chat', component: _import('chat/chat'), name: 'chat', meta: {title: '聊天', requiresAuth: true}},
    {path: '/cart', component: _import('cart/cart'), name: 'cart', meta: {title: '购物车', requiresAuth: true}},
    {path: '/user', component: _import('user/user'), name: 'user', meta: {title: '用户主页', requiresAuth: true}}
  ]

  // beforeEach (to, from, next) {
  //   console.log(11)
  //   if (to.matched.some(record => record.meta.requiresAuth)) {
  //     // debugger
  //     let token = Vue.cookie.get('token')
  //     if (!token || !/\S/.test(token)) {
  //       next({ name: 'login' })
  //     }else {
  //       next()
  //     }
  //   }else {
  //     next()
  //   }
  //
  // }
}


const router = new Router({
  routes: globalRoutes.concat(mainRoutes),
  mode: 'history',
})
router.beforeEach((to, from, next) => {
  console.log(11)
  if (to.matched.some(record => record.meta.requiresAuth)) {
    let token = Vue.cookie.get('token')
    if (!token || !/\S/.test(token)) {
      next({name: 'login'})
    } else {
      next()
    }
  } else {
    next()
  }
})

/**
 * 判断当前路由类型, global: 全局路由, main: 主入口路由
 * @param {*} route 当前路由
 */
function fnCurrentRouteType(route) {
  var temp = []
  for (var i = 0; i < globalRoutes.length; i++) {
    if (route.path === globalRoutes[i].path) {
      return 'global'
    } else if (globalRoutes[i].children && globalRoutes[i].children.length >= 1) {
      temp = temp.concat(globalRoutes[i].children)
    }
  }
  return temp.length >= 1 ? fnCurrentRouteType(route, temp) : 'main'
}


export default router
